package com.senlingspace.framework.manager;

import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.senlingspace.common.utils.Threads;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 异步任务管理器
 *
 * @author senlingspace
 */
public class AsyncManager {
    private static final Logger logger = LoggerFactory.getLogger(AsyncManager.class);
    private static AsyncManager instance = new AsyncManager();

    private ScheduledExecutorService executor;

    private AsyncManager() {
        // 默认构造函数
    }

    public static AsyncManager me() {
        return instance;
    }

    public void initialize(ScheduledExecutorService executor) {
        this.executor = executor;
        logger.info("ScheduledExecutorService initialized successfully");
    }

    public void execute(TimerTask task) {
        executor.schedule(task, 10, TimeUnit.MILLISECONDS);
    }

    public void shutdown() {
        logger.info("Shutting down AsyncManager");
        Threads.shutdownAndAwaitTermination(executor);
    }
}
